package com.whpost.service.service.stamp;

import com.whpost.service.dao.stamp.StampCardOrderMapper;
import com.whpost.service.dao.stamp.StampCardOrderMxMapper;
import com.whpost.service.dao.stamp.StampHdDhmMapper;
import com.whpost.service.dao.stamp.StampXmMapper;
import com.whpost.service.dao.t.MyDao;
import com.whpost.service.dao.wx.TWxOrderDao;
import com.whpost.service.entity.stamp.StampCardOrder;
import com.whpost.service.entity.stamp.StampXm;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.entity.wx.TWxOrder;
import com.whpost.service.tools.ToolBean;
import com.whpost.service.tools.Url;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: Yin Zhuoyi
 * @Date: 2023/4/3 10:07
 */
@Service
public class StampOrderServices {


    @Resource
    private TWxOrderDao tWxOrderDao;

    @Resource
    private StampCardOrderMapper stampCardOrderDao;

    @Resource
    private StampCardOrderMxMapper stampCardOrderMxDao;

    @Resource
    private StampXmMapper stampXmMapper;

    @Resource
    private StampHdDhmMapper stampHdDhmMapper;


    @Resource
    private MyDao myDao;
    /**
     * 处理订单业务
     * @param tWxOrder
     * @return
     */
    @Transactional
    public String dealDdxxByWxOrder(TWxOrder tWxOrder)
    {
        TWxOrder tWxOrder1 = new TWxOrder();
        StampCardOrder stampCardOrder = new StampCardOrder();

        String rtn = "";
        String id = "";
        String tname = "";
        try {
            tWxOrder1 = tWxOrderDao.selectById(tWxOrder.getLsh());
            stampCardOrder = stampCardOrderDao.selectById(tWxOrder.getZblsh());
            String ywclState = tWxOrder1.getYwclState();
            if (StringUtils.isEmpty(ywclState)) ywclState = "0";
            if (ywclState.equals("1"))
            {
                return "{\"code\":\"30002\",\"msg\":\"订单业务已经完成，不要重复执行！\"}";
            }else{
                ToolBean.info("开始处理stamp_card_order[订单主键："+tWxOrder1.getZblsh()+"]业务逻辑，准备更新isfk标志...begin");
                rtn = "";
                String isfk = stampCardOrder.getIsfk();
                if ("1".equals(isfk)){
                    rtn = "{\"code\":\"30002\",\"msg\":\"订单表付款标志已经更改过，不要重复更新！\"}";
                }else{
                    stampCardOrder.setIsfk("1");
                    stampCardOrder.setFkrq(ToolBean.getToday());
                    stampCardOrderDao.updateById(stampCardOrder);

                    //如果打印，需要写打印表
                    String lqfs = stampCardOrder.getLqfs();
                    SimpleDateFormat dfasd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                    if ("1".equals(lqfs)){
                    }

                    rtn = "{\"code\":\"200\",\"msg\":\"完成！\"}";
                }
                tWxOrder1.setYwclState("1");
                tWxOrderDao.updateById(tWxOrder1);
                return rtn;

            }
        }
        catch (Exception e)
        {
            ToolBean.info("dealDdxxByWxOrder发生错误！"+e.toString());
            throw new RuntimeException("dealDdxxByWxOrder发生错误！"+e.toString());
        }
    }

    @Transactional
    public ResultInfo dealDdxxByDhm(Long id,String dhm)
    {
        StampCardOrder stampCardOrder = new StampCardOrder();
        ResultInfo resultInfo = new ResultInfo();
        String rtn = "";
        try {
            stampCardOrder = stampCardOrderDao.getById(id);

            //订单是否存在
            if (null ==stampCardOrder){
                return new ResultInfo(500, "订单不存在！",null);
            }

            if ("1".equals(stampCardOrder.getIsfk())){
                return new ResultInfo(500, "订单已经付款或者完成兑换！",null);
            }

            StampXm stampXm = new StampXm();
            stampXm = stampXmMapper.selectById(stampCardOrder.getStampHd().getXmId());

            if (null ==stampXm){
                return new ResultInfo(500, "项目不存在！",null);
            }

            String dhm_jm = Url.encrypt(dhm);

            int sl = stampHdDhmMapper.payByDhm(stampXm.getId(),stampCardOrder.getHdId(),dhm_jm,id);

            if (sl>0){
                stampCardOrder.setIsfk("1");
                stampCardOrder.setFkfs("2");
                stampCardOrder.setFkrq(ToolBean.getToday());
                stampCardOrderDao.updateById(stampCardOrder);
                return new ResultInfo(0, "兑换完成！",null);
            }else{
                return new ResultInfo(500, "兑换失败！请核对兑换码！",null);
            }
        }
        catch (Exception e)
        {
            ToolBean.info("dealDdxxByDhm发生错误！"+e.toString());
            throw new RuntimeException("dealDdxxByDhm发生错误！"+e.toString());
        }
    }
}
